package com.example.its2021606134chh.MyCarModule;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.its2021606134chh.R;

public class MyCarShow extends View {
    private String text = "test";
    private String text2 = "test2";
    private String text3 = "test3";
    private int textsize = 50;
    private int textsize2 = 50;
    private int textsize3 = 50;
    private int color = R.color.colorTargetNormal;
    public MyCarShow(Context context) {
        super(context);
    }
    public void setText2(int threshold, String reality) {
        this.text2 = reality;
        int iReality = Integer.parseInt(reality);
        if( iReality > threshold){  //实际值大于阈值
            this.color = R.color.colorTargetNormal;
            this.text3 = "正常";
        } else {
            this.color = R.color.colorTargetWarning;
            this.text3 = "警告";
        }
    }

    public MyCarShow(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCarShow);
        text = typedArray.getString(R.styleable.MyCarShow_MyCarText);
        text2 = typedArray.getString(R.styleable.MyCarShow_MyCarText2);
        text3 = typedArray.getString(R.styleable.MyCarShow_MyCarText3);
        textsize = Integer.parseInt(typedArray.getString(R.styleable.MyCarShow_MyCarTextSize));
        textsize2 = Integer.parseInt(typedArray.getString(R.styleable.MyCarShow_MyCarTextSize2));
        textsize3 = Integer.parseInt(typedArray.getString(R.styleable.MyCarShow_MyCarTextSize3));
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(color, null));
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(5);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        canvas.drawRoundRect(1, 1, width, height, 10, 10, paint);
        paint.setAntiAlias(false);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(width/2 + 100, height/2 + 50, 100, paint);
        paint.setTextSize(textsize);
        paint.setStrokeWidth(5);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
        Paint.FontMetrics metrics = paint.getFontMetrics();
        canvas.drawText(text, width/2 - 150, height/2 - metrics.top/2 - metrics.bottom/2 - 100, paint);
        paint.setTextSize(textsize2);
        paint.setStrokeWidth(5);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(text2, width/2 + 50, height/2 - metrics.top/2 - metrics.bottom/2 + 50, paint);
        paint.setTextSize(textsize3);
        paint.setStrokeWidth(5);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(text3, width/2 - 150, height/2 - metrics.top/2 - metrics.bottom/2 + 50, paint);
    }
    @Override
    public void invalidate() {
        super.invalidate();
    }
}
